package com.xinmao.quantitative.trad.rule;

import org.ta4j.core.Rule;
import org.ta4j.core.TradingRecord;
import org.ta4j.core.num.DecimalNum;
import org.ta4j.core.num.Num;
import org.ta4j.core.rules.AbstractRule;

import java.util.Map;

public class VotingRule extends AbstractRule {


    private final Map<Rule, Num>  rules;

    public VotingRule(Map<Rule, Num> rules) {
        this.rules = rules;
    }

    @Override
    public boolean isSatisfied(int i, TradingRecord tradingRecord) {
        if (rules.isEmpty()){
            throw new RuntimeException("No rules defined");
        }
        Num voting = DecimalNum.ZERO;
        for (Rule rule : rules.keySet()) {
            if (rule.isSatisfied(i, tradingRecord)) {
                voting = voting.plus(rules.get(rule));
            }
        }
        return voting.isGreaterThanOrEqual(DecimalNum.valueOf(1));
//        return voting.dividedBy(DecimalNum.valueOf(rules.size())).isGreaterThanOrEqual(DecimalNum.valueOf(0.23));
    }
}
